package com.hospital.pharmacy.controller;

import com.alibaba.fastjson.JSON;
import com.hospital.pharmacy.service.OverdueRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: Hospital
 * @description: 过期
 * @author: 杨曼珍
 * @create: 2019-01-10 19:50
 **/
@Controller
@RequestMapping(value = "/overdue")
public class OverdueRecordController {
    @Autowired
    OverdueRecordService overdueRecordService;

    @RequestMapping(value = "/all",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @ResponseBody
    public String getOverRecord(){
        return overdueRecordService.getAllOver();

    }
    @RequestMapping(value = "/mdcName",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String getOverRecord(@RequestBody String data){
        String string = JSON.parseObject(data).getString("mdcName");
        return overdueRecordService.getOverByMedName(string);

    }
    @RequestMapping(value = "/date",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String getOverByDate(@RequestBody String data)throws ParseException {
        String string = JSON.parseObject(data).getString("ovdRecDate");
        Date date=new SimpleDateFormat("yyyy-MM-dd").parse(string);
        return overdueRecordService.getOverByDate(date);

    }
    @RequestMapping(value = "/person",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String getOverByPerson(@RequestBody String data){
        String string = JSON.parseObject(data).getString("ovdRecPerson");
        return overdueRecordService.getOverByPerson(string);

    }
    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody String data)throws ParseException{
        Integer mdcId = JSON.parseObject(data).getInteger("mdcId");
        Integer ovdRecCount = JSON.parseObject(data).getInteger("ovdRecCount");
        String ovdRecDate = JSON.parseObject(data).getString("ovdRecDate");
        Date date=new SimpleDateFormat("yyyy-MM-dd").parse(ovdRecDate);
        String ovdRecIllustration = JSON.parseObject(data).getString("ovdRecIllustration");
        String ovdRecPerson = JSON.parseObject(data).getString("ovdRecPerson");
        String ovdRemark = JSON.parseObject(data).getString("ovdRemark");

        return overdueRecordService.add(mdcId,ovdRecCount,date,ovdRecIllustration,ovdRecPerson,ovdRemark);

    }
    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody String data)throws ParseException{
        Integer id = JSON.parseObject(data).getInteger("id");
        Integer ovdRecCount = JSON.parseObject(data).getInteger("ovdRecCount");
        String ovdRecDate = JSON.parseObject(data).getString("ovdRecDate");
        Date date=new SimpleDateFormat("yyyy-MM-dd").parse(ovdRecDate);
        String ovdRecIllustration = JSON.parseObject(data).getString("ovdRecIllustration");
        String ovdRemark = JSON.parseObject(data).getString("ovdRemark");
        return overdueRecordService.update(id,ovdRecCount,date,ovdRecIllustration,ovdRemark);

    }
    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody String data){
        Integer string = JSON.parseObject(data).getInteger("id");
        return overdueRecordService.delete(string);

    }
}
